/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pscombine;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import pscombine.database.DBManager;
import pscombine.extractor.Category;
import pscombine.extractor.Extractor;
import pscombine.extractor.LangMap;
import pscombine.ftp.FTP;

/**
 *
 * @author srkrishnan
 */
public class Pscombine {

    Extractor extractor = null;
    DBManager primarydb = null;
    DBManager[] secdb = null;
    String url = "jdbc:mysql://localhost:3306/";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "support";
    Connection src = null;
    Connection dest = null;

    public Pscombine() {
        start();
    }

    public void start() {
        try {
            Configuration config = new PropertiesConfiguration("my.properties");

            int shopcount = getShopCount(config);

            //shopcount=1;
            
            url = "jdbc:mysql://" + config.getString("shopP.dbhost") + ":" + config.getString("shopP.dbport") + "/";
            userName = config.getString("shopP.dbuser");
            password = config.getString("shopP.dbpassword");
            primarydb = new DBManager(url, config.getString("shopP.database"), driver, userName, password);
            primarydb.connectDB();

            secdb = new DBManager[shopcount];
            for (int i = 0; i < shopcount; i++) {
                url = "jdbc:mysql://" + config.getString("shop"+(i+1)+".dbhost") + ":" + config.getString("shop"+(i+1)+".dbport") + "/";
                userName = config.getString("shop"+(i+1)+".dbuser");
                password = config.getString("shop"+(i+1)+".dbpassword");
                secdb[i] = new DBManager(url, config.getString("shop" + (i + 1) + ".database"), driver, userName, password);
                secdb[i].connectDB();
            }

            for (int i = 0; i < shopcount; i++) {
                extractor = new Category();

                src = secdb[i].getConnection();
                dest = primarydb.getConnection();
                HashMap<Integer, Integer> langmap = LangMap.generateLangMap(dest, src);
                HashMap<Integer, Integer> imageidmap = new HashMap<Integer, Integer>();

                extractor.copyRecords(src, dest, "shop" + (i + 1), "shopP", langmap, imageidmap);

            }
        } catch (ConfigurationException ex) {
            Logger.getLogger(Pscombine.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new Pscombine();
    }

    private int getShopCount(Configuration config) {
        int maxshop = config.getInt("shop.maxshop");
        for (int i = 1; i <= maxshop; i++) {
            if (config.getString("shop" + i + ".hostname") == null) {
                return i - 1;
            }
        }
        return 0;
    }
}
